'use strict';
/**
 * @name 需求合作的controller
 * @author yud
 */
angular.module('metalabPartnerApp')
    .controller('DemandCooperationCtrl', ['$rootScope', '$scope', '$state', '$stateParams', 'MetaConstants', 'MetaResources', 'MetaRemind',
        function ($rootScope, $scope, $state, $stateParams, MetaConstants, MetaResources, MetaRemind) {

            //variables

            $scope.clicked = [];
            $scope.stepValid = [];

            //初始化提交字段

            $scope.technologyFields = MetaConstants.technologyFields;

            $scope.formData = {
                'effectTags': [],
                'numberOfApplying': undefined,
                'numberOfApplied': undefined,
                'attachments': [],
                'title': undefined,
                'origin': '中国',
                'areas': [],
                'cooperationWays': [],
                'cooperationPrice': [],
                'technologyForm': undefined,
                'patentState': undefined,
                'maturity': undefined,
                'details': undefined,
                'advantages': undefined,
                'indicators': undefined,
                'applicableObjects': undefined,
                'detailsOfCases': undefined,
                'video': undefined,
                'contactInfo': {
                    'name': $scope.USER.name,
                    'phone': $scope.USER.mobile
                },
                'relationWithOwner': undefined,
                'ownedBy': undefined,
                'organization': undefined,
                'postOrTitle': undefined,
                'tags': ['合作'],
                'online': false,
                'state': '新提交'
            };

            MetaResources.technology.origin(null, function (data) {
                $scope.origins = data;
            }, function (error) {
                MetaRemind.error(error);
            });

            $scope.verifiyModel = function (data) {
                if ($rootScope.openOfficerVerifyModal()) {
                    data = true;
                } else {
                    data = false;
                }
            };

            $scope.submit = function () {

                var states = ['one', 'two', 'three'];
                var remind = ['一', '二', '三'];

                for (var i = 0; i < $scope.stepValid.length; ++i) {
                    if (!$scope.stepValid[i]) {
                        $state.go('^.' + states[i]);
                        MetaRemind.info('请完成步骤' + remind[i] + '的填写!');
                        return;
                    }
                }

                MetaResources.technology.create({userId: '_self'}, $scope.formData, function (data) {
                    /**
                     * 根据提交的解决方案上传至对应的接口
                     * $stateParams里可以获取demandId
                     */
                    MetaResources.process.demandProcessAdd({technologyId: data.id}, {demandId: $stateParams.demandId}, function (process) {
                        MetaRemind.success('你已成功提交解决方案，请耐心等待负责人的联系');
                        $state.go('^.success', {
                            toState: 'meta.technology.list.released',
                            moduleName: 'technology',
                            moduleId: data.id
                        });
                    }, function (err) {
                        MetaRemind.error('出错了', err);
                    });
                }, function (error) {
                    console.log(error);
                })
            };

            $scope.stepOneNext = function (valid) {
                $scope.clicked[0] = true;
                if (valid && $scope.formData.cooperationWays.length !== 0) {
                    $scope.stepValid[0] = true;
                    $state.go('^.two');
                } else {
                    MetaRemind.error('请完整填写解决方案概要信息');
                }
            };

            $scope.stepTwoNext = function (valid) {
                $scope.clicked[1] = true;
                if (valid) {
                    $scope.stepValid[1] = true;
                    $state.go('^.three');
                } else {
                    MetaRemind.error('请完整填写解决方案详细信息');
                }
            };

            $scope.stepThreeNext = function (valid) {

                $scope.clicked[2] = true;
                if (valid) {
                    $scope.stepValid[2] = true;
                    $scope.submit();
                } else {
                    MetaRemind.error('请完整填写解决方案相关者信息');
                }
            };
        }]);
